(function() {
	var Event = {};
	
	Event.observe = function(elm, event, func, b) {
		var _b = (typeof(b) != 'undefined' &&  b) || false;
		if (elm.addEventListener){
		  elm.addEventListener(event, func, b);
		} else if (elm.attachEvent){
		  elm.attachEvent('on'+event, func);
		}
	};

	var getBrowserWidth = function() {
    if ( window.innerWidth ) { return window.innerWidth; }
    else if ( document.documentElement && document.documentElement.clientWidth != 0 ) { return document.documentElement.clientWidth; }
    else if ( document.body ) { return document.body.clientWidth; }
    return 0;
	}
	var getBrowserHeight = function() {
    if ( window.innerHeight ) { return window.innerHeight; }
    else if ( document.documentElement && document.documentElement.clientHeight != 0 ) { return document.documentElement.clientHeight; }
    else if ( document.body ) { return document.body.clientHeight; }
    return 0;
	}

	var SearchBar = function(height){
		this.initialize(height);
	};
	
	SearchBar.prototype = {
		bar : null,
		link : {},
		h : 32,
		
		initialize: function(height) {
			if(typeof(height) != 'undefined') this.h = height;
		},
		
		show: function(){
			if(this.bar) document.body.removeChild(this.bar);
			
			var w = getBrowserWidth();
			var h = getBrowserHeight();
			
			this.bar = document.createElement('div');
			this.bar.style.width = w+"px";
			this.bar.style.height = this.h+"px";
			this.bar.style.position = 'fixed';
			this.bar.style.top = (h-this.h)+'px';
			this.bar.style.left = '0px';
			this.bar.style.background = 'yellow';
			document.body.appendChild(this.bar);
		},
		
		set:function(res){
			var cnt = (res.length > 3 ? 3: res.length);
			var out = '';
			
			var ww = 100;
			var wh = 100;
			
			var _w = parseInt(getBrowserWidth() / 3)+'px';
			for(var i = 0; i < cnt; i++) {
				out += '<div style="float:left;width:'+_w+';margin-top:2px">';
				out += '&nbsp;&nbsp;<a class="atpamoolink" href="'+res[i].link+'" target="_top">'+res[i].title+'</a></div>';
			}
			
			out += '<div style="clear:both"></div>';
			this.bar.innerHTML = out;
			
			var that = this;
			
			for(var i = 0; i < cnt; i++) {
				var obj = document.getElementsByClassName('atpamoolink')[i];
				Event.observe(obj, 'mouseover', function(e) {
					var elm = e.target || e.srcElement;
					var link = elm.href;
					if(console && console.log) console.log(link);
					
					that.showLink(link);
				},false);
				Event.observe(obj, 'mouseout', function(e) {
					var elm = e.target || e.srcElement;
					var link = elm.href;
					that.removeLink(link);
				},false);
			}
		},
		
		showLink: function(link) {
			if(this.link[link]) {
				this.link[link].style.display = 'block';
				return;
			}
			
			var w = 400;
			var h = 250;
			
			this.link[link] = {}
			
			this.link[link] = document.createElement('div');
			this.link[link].style.width = w +'px';
			this.link[link].style.height = h +'px';
			this.link[link].style.background = 'white';
			this.link[link].style.position = 'fixed';
			this.link[link].style.left = '10px';
			this.link[link].style.border = '1px solid gray';
			this.link[link].style.top = (getBrowserHeight() - this.h - h)+'px';
			
			document.body.appendChild(this.link[link]);
			
			var iframe = document.createElement('iframe');
			iframe.src = link;
			iframe.width = '100%';
			iframe.height = '100%';
			iframe.border = '0';
			iframe.frameBorder = 'no';
			
			this.link[link].appendChild(iframe);
			//this.link.innerHTML = link;
		},
		
		removeLink: function(link) {
			if(this.link[link]) this.link[link].style.display = 'none';
		}
	};
	
//	Event.observe(window, 'click', function(e){alert('click');});
	Event.observe(window, 'load', function(e) {
		var bar = new SearchBar();
		bar.show();
		bar.set([{title:'goo', link:'http://www.goo.ne.jp'},
			{title:'ocn', link:'http://www.ocn.ne.jp'},
			{title:'yahoo', link:'http://www.yahoo.co.jp'}
		]);
	}, false);
})();